/** Block represents several page **/

package qp.utils;

import java.util.Vector;
import java.io.Serializable;

import qp.operators.Debug;

public class Block  implements Serializable{


    /**
	 * 
	 */


	public int maxNumBatchInBlock; //how many batch can be included in leftBlock
    

    Vector<Batch> pageBlock; // store the batch object
//    Vector tupleSizeOfEachPage; //store the number of tupeles in each page
    
//    int existBatchInBlock; //how many batch have bee included in pageBlock	

    public int existTupleInBlock; //how many tuples is included by this block    
    
	public Block (int numberBatch)
		{
		 maxNumBatchInBlock = numberBatch;
//		 existBatchInBlock=0; 
		 pageBlock = new Vector(maxNumBatchInBlock);
		existTupleInBlock = 0;
//		 tupleSizeOfEachPage = new Vector(maxNumBatchInBlock);
		}

	//add by wang suyun 2009.11.05
	public Batch remove(int index) {
		existTupleInBlock=existTupleInBlock-pageBlock.elementAt(index).size();
		
		return pageBlock.remove(index);
	}
	
	
	public void add (int index, Batch page)
		{
		pageBlock.add(index, page);
//		existBatchInBlock++;
		existTupleInBlock+=page.size();
//		tupleSizeOfEachPage.add(index,page.size());
		}
	public void  emptyBlock()
		{
//		existBatchInBlock = 0;
		existTupleInBlock=0;
		}
	public boolean isEmpty()
		{
		if (existTupleInBlock==0)
			return true;
		else
			return false;
		}
	
	//wang suyun 2009.11.02
	public void clear() {
		pageBlock.clear();
		existTupleInBlock=0;
	}

	//find the index of Batch which contain the smallest value in the block
	//add by wang suyun 2009.11.05
	public int findMinFromSortedBatch(int index) {
		int minimal=0;
		int compareFlag=0;
		for(int i=0;i<pageBlock.size();i++) {
			if(pageBlock.elementAt(i).size()==0) {
				continue;
			}
			else{
				try {
					compareFlag=Tuple.compareTuples(pageBlock.elementAt(minimal).elementAt(0), pageBlock.elementAt(i).elementAt(0), index);
					if(compareFlag>0) {
						minimal=i;
					}
				}
				catch (ArrayIndexOutOfBoundsException e) {
					// TODO: handle exception
					minimal=i;
					continue;
				}
				catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		/*for debug
		System.out.println("=======all tuples=======");
		for(int i=0;i<pageBlock.size();i++) {
			System.out.println("-----NO: "+i+" batch-----");
			Debug.PPrint(pageBlock.elementAt(i));
		}
		System.out.println("=======minimal tuples=======");
		Debug.PPrint(pageBlock.elementAt(minimal).elementAt(0));
		*///end debug
		return minimal;
	}
	
	// add by wang suyun 2009.11.05
	public Batch batchAt(int index) {
		return pageBlock.elementAt(index);
	}
	// add by wang suyun 2009.11.05
	public int size() {
		return pageBlock.size();
	}
	
	public int sizeTuple()
		{
		return existTupleInBlock;
		}

	public Tuple  elementAt(int index)
		{
		       if(index>existTupleInBlock)  //request tuple is not included 
		       	{
		       	   return null;
		       	}
			   else
			{
			       Batch temp_batch;
			   	for(int i=0; i<pageBlock.size(); i++) //find the require tuple by scan the size vector
			   		{
			   			temp_batch = (Batch) pageBlock.elementAt(i);
			   			if(index<temp_batch.size()) //find the taget batch in block
			   				{
			   				return (Tuple) temp_batch.elementAt( index);
			   				}
						else  //reduce the index, and wait for next batch
							{
							index -= temp_batch.size();
							}
			   		}

				return null;

			 }
			
		}
}




